# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2022/10/23 17:23
desc:
'''
from appium.webdriver.common.appiumby import AppiumBy
from proj_spec.CLUE.mobile_po.clue_app_base_page import CLUEAppBasePage
from proj_spec.CLUE.mobile_po.login_page import LoginPage


class MyPage(CLUEAppBasePage):
    _main_page_locator_android = (AppiumBy.ANDROID_UIAUTOMATOR, 'UiSelector().text("首页")')
    _main_page_locator_ios = (AppiumBy.IOS_CLASS_CHAIN, '**/XCUIElementTypeButton[`label == "首页"`]')
    _account_setting_locator_android = (AppiumBy.ANDROID_UIAUTOMATOR, 'UiSelector().text("账户设置")')
    _safe_logout_locator_android = (AppiumBy.ANDROID_UIAUTOMATOR, 'UiSelector().text("安全退出")')
    _confirm_logout_locator_android = (AppiumBy.ANDROID_UIAUTOMATOR, 'UiSelector().text("确认")')


    def logout(self):
        self.find_element_and_click(getattr(self, '_account_setting_locator_' + self.os))
        self.find_element_and_click(getattr(self, '_safe_logout_locator_' + self.os))
        self.find_element_and_click(getattr(self, '_confirm_logout_locator_' + self.os))

    def login(self, username, password):
        login_page = LoginPage(self.driver)
        login_page.login(username, password)

    def to_main(self):
        """转到首页

        :return:
        """
        self.find_element_and_click(getattr(self, '_main_page_locator_' + self.os))

    def is_logged_in(self, account_name=None):
        """判断是否已登录

        :param account_name: 账号名，默认None则仅判断app是否已登录（无论哪个账号）

        :return: None
        """
        if account_name is None:
            if self.find_element(getattr(self, '_account_setting_locator_' + self.os)):
                return True
            else:
                return False
        else:
            if self.find_element((AppiumBy.ANDROID_UIAUTOMATOR, 'UiSelector().text("%s")'%account_name)):
                return True
            else:
                return False